package com.wk.controller.admin;

import com.wk.pojo.Class;
import com.wk.pojo.User;
import com.wk.service.Adminservice;
import com.wk.utils.responseresult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: thesis
 * @description: 管理员的登录类
 * @author: 王侃
 * @create: 2020-03-09 22:27
 **/
@Controller
public class Adminlogincontroller {

    @Autowired
    Adminservice adminservice;

    //返回管理员登录界面
    @RequestMapping("/admin_login")
    public String admin_login()
    {
        return "admin/login";
    }

    //返回管理员主界面
    @RequestMapping("/admin/main")
    public String admin_main()
    {
        return "admin/main";
    }

    //返回所有的班级信息
    @ModelAttribute("allclass")
    public List<Class> all_class()
    {
        return adminservice.selectbyallclass();
    }

    //登录提交
    @RequestMapping("/admin_loginsubmit")
    public String loginsubmit(String username, String password, Model model)
    {
        //获取当前用户subject
        Subject subject= SecurityUtils.getSubject();
        //进行Md5加密，第一个参数为密码，第二参数是以用户名为盐，第三个参数是加密的次数
        Md5Hash md5registepassword=new Md5Hash( password, username,5);
        //封装用户的登录数据
        UsernamePasswordToken token=new UsernamePasswordToken(username,md5registepassword.toString());
        try {
            subject.login(token);
            return "redirect:/admin/main";

        }catch (UnknownAccountException e)//用户名不存在
        {
            model.addAttribute("msg","用户名不存在");
            return "admin/login";
        }catch (IncorrectCredentialsException e)
        {
            model.addAttribute("msg","密码错误");
            return "admin/login";
        }
    }

    //跳转到学生用户管理界面
    @RequestMapping("/admin/user_stu")
    public String user_stu(Model model)
    {
        List<User>  users= adminservice.selectbyallstu();
        model.addAttribute("users",users);
        return "admin/user_stu";
    }

    //跳转到学生用户管理界面
    @RequestMapping("/admin/user_teacher")
    public String user_teacher(Model model)
    {
        List<User>  users= adminservice.selectbyallteacher();
        model.addAttribute("users",users);
        return "admin/user_teacher";
    }

    //修改用户的状态
    @RequestMapping("/admin/change/status")
    @ResponseBody
    public responseresult change_status(Integer id,Integer status,Model model)
    {
        if (status==0)
        {
            status=1;
        }
        else
        {
            status=0;
        }

        adminservice.updatebyid_status(id, status);
        return responseresult.success();
    }

    //跳转到班级管理界面
    @RequestMapping("/admin/type_class")
    public String type_class(Model model)
    {
        List<Class> classes=adminservice.selectbyallclass();
        model.addAttribute("classes",classes);
        return "admin/type_class";
    }

    //跳转到班级添加界面
    @RequestMapping("/admin/type/class/add")
    public String class_add()
    {
        return "admin/type_class_add";
    }

    //班级添加界面提交
    @RequestMapping("/admin/type/class/addsubmit")
    public String class_addsubmit(String class_name)
    {
        adminservice.insertintoclass(class_name);
        return "common/success";
    }

    //修改班级前获得信息
    @RequestMapping("/admin/type/class/getinformation")
    public String class_getinformation(Integer id,Model model)
    {
        Class myclass=adminservice.selectbyidclass(id);
        model.addAttribute("class",myclass);
        return "admin/type_class_update";
    }

    //班级修改提交
    @RequestMapping("/admin/type/class/updatesubmit")
    public String class_updatesubmit(Class myclass)
    {
        adminservice.updatebyclass(myclass);
        return "common/success";
    }

    //教师退出登录
    @RequestMapping("/admin/logout")
    public String logout(HttpSession session)
    {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "admin/login";
    }


}
